/**
 * 
 */
package com.zito.utils;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.UUID;

/**
 * 
 * <p>
 * Title: 半月山庄工程核心模块
 * </p>
 * <p>
 * Description: StringUtils
 * </p>
 * <p>
 * date: Dec 23, 2008 - 10:44:20 PM
 * </p>
 * 
 * @author 蒋云海
 * @version 2.0
 */
public class StringUtils {

	private static final char[] QUOTE_ENCODE = "&quot;".toCharArray();
	private static final char[] AMP_ENCODE = "&amp;".toCharArray();
	private static final char[] LT_ENCODE = "&lt;".toCharArray();
	private static final char[] GT_ENCODE = "&gt;".toCharArray();

	private static final String FOLDER_SEPARATOR = "/"; // folder separator
	private static final String WINDOWS_FOLDER_SEPARATOR = "\\"; // Windows
	// folder
	// separator
	private static final String CURRENT_PATH = "."; // current folder
	private static final String TOP_PATH = ".."; // top folder

	/**
	 * 字符串替换，采用了Spring中的方式
	 * 
	 * @param inString
	 *            String
	 * @param oldPattern
	 *            String
	 * @param newPattern
	 *            String
	 * @return String
	 */
	public static String replace(String inString, String oldPattern,
			String newPattern) {
		if (inString == null) {
			return null;
		}
		if (oldPattern == null || newPattern == null) {
			return inString;
		}

		StringBuffer sbuf = new StringBuffer();
		// output StringBuffer we'll build up
		int pos = 0; // our position in the old string
		int index = inString.indexOf(oldPattern);
		// the index of an occurrence we've found, or -1
		int patLen = oldPattern.length();
		while (index >= 0) {
			sbuf.append(inString.substring(pos, index));
			sbuf.append(newPattern);
			pos = index + patLen;
			index = inString.indexOf(oldPattern, pos);
		}
		sbuf.append(inString.substring(pos));

		// remember to append any characters to the right of a match
		return sbuf.toString();
	}

	/**
	 * 编码类型转换，一般用于中文字符的传递. 使用实例：
	 * 
	 * <pre>
	 * StringUtils.conversionUnicode(strIn, &quot;ISO8859_1&quot;, &quot;utf-8&quot;);
	 * </pre>
	 * 
	 * @param strIn
	 * @param sourceType
	 * @param converType
	 * @return String
	 */
	public static String conversionUnicode(String strIn, String sourceType,
			String converType) {
		String strOut = null;
		if (strIn == null || (strIn.trim()).equals("")) {
			return "";
		}
		try {
			byte[] b = strIn.getBytes(sourceType);
			strOut = new String(b, converType);
		} catch (Exception e) {
		}
		return strOut;
	}

	/**
	 * 将Unicode转为UTF8编码.
	 * 
	 * @param strIn
	 * @return String
	 */
	public static String UnicodeToUTF8(String strIn) {
		return conversionUnicode(strIn, "ISO8859_1", "utf-8");
	}

	/**
	 * 将Unicode转为GB编码，该方法返回的是GB2312
	 * 
	 * @param strIn
	 *            String
	 * @return String
	 */
	public static String UnicodeToGB(String strIn) {
		return conversionUnicode(strIn, "ISO8859_1", "GB2312");
	}

	/**
	 * 该方法是将UTF8编码转换为Unicode编码
	 * 
	 * @param strIn
	 *            String
	 * @return String
	 */
	public static String UTF8ToUnicode(String strIn) {
		return conversionUnicode(strIn, "utf-8", "ISO8859_1");
	}

	/**
	 * 将GB2312转为Unicode编码.
	 * 
	 * @param strIn
	 *            String
	 * @return String
	 */
	public static String GBToUnicode(String strIn) {
		return conversionUnicode(strIn, "GB2312", "ISO8859_1");
	}

	/**
	 * 将NULL转为空字符.
	 * 
	 * @param str
	 *            String
	 * @return String
	 */
	public static String getNullString(String str) {
		return (str == null) ? "" : str;
	}

	/**
	 * 得到输入框字符的准确长度，正确识别中文字符。
	 * 
	 * @param text
	 * @return <code>int</code> 返回指定字符串的长度，能正确识别中文，一个中文当作两个英文字符
	 */
	public static int strLength(String text) {
		int k = 0;
		for (int i = 0; i < text.length(); i++) {
			if (text.charAt(i) > 255) {
				k += 2;
			} else {
				k += 1;
			}
		}
		return k;
	}

	/**
	 * 使用charsToDelete删除字符串inString中所有的pattern 该方法严格匹配大小写,
	 * <p>
	 * 
	 * <pre>
	 * StringUtils.deleteAny(&quot;什么WEB重构技术&quot;,&quot;E技&quot;) return &quot;什么WB重构术&quot;
	 * </pre>
	 * 
	 * @param inString
	 * @param charsToDelete
	 * @return String
	 */
	public static String deleteAny(String inString, String charsToDelete) {
		if (inString == null || charsToDelete == null) {
			return inString;
		}
		StringBuffer out = new StringBuffer();
		for (int i = 0; i < inString.length(); i++) {
			char c = inString.charAt(i);
			if (charsToDelete.indexOf(c) == -1) {
				out.append(c);
			}
		}
		return out.toString();
	}

	/**
	 * 删除字符串inString中所有的pattern 该方法严格匹配大小写 与deleteAny不同
	 * 
	 * @param inString
	 * @param pattern
	 * @return String
	 */
	public static String delete(String inString, String pattern) {
		return replace(inString, pattern, "");
	}

	/**
	 * 得到最右指定char后的所有字符串,一般可用于检查文件名后缀.
	 * <p>
	 * 
	 * <pre>
	 * String oldText1 = &quot;镇江;电;脑科技&quot;;
	 * char oldText2 = ';';
	 * StringUtils.unqualify(oldText1,oldText2) returns &quot;脑科技&quot;
	 * </pre>
	 * 
	 * "this:name:is:qualified" returns "qualified" if using a ':' separator.
	 * 
	 * @param qualifiedName
	 *            the qualified name
	 * @param separator
	 *            the separator
	 */
	public static String unqualify(String qualifiedName, char separator) {
		return qualifiedName
				.substring(qualifiedName.lastIndexOf(separator) + 1);
	}

	/**
	 * 检查 prefix 是否与 str 的最左方匹配，该方法忽略大小写 ignoring upper/lower case.
	 * <p>
	 * 
	 * <pre>
	 * StringUtils.startsWithIgnoreCase(&quot;镇江立 天软件科技 有限公司&quot;,&quot;镇江立 天&quot;) = true
	 * StringUtils.startsWithIgnoreCase(&quot;镇江立 天软件科技 有限公司&quot;,&quot;镇江立天&quot;) = false
	 * StringUtils.startsWithIgnoreCase(&quot;abcdefg&quot;,&quot;Abc&quot;) = true
	 * StringUtils.startsWithIgnoreCase(&quot;abcdefg&quot;,&quot;abc&quot;) = true
	 * StringUtils.startsWithIgnoreCase(&quot;abcdefg&quot;,&quot;aBC&quot;) = true
	 * </pre>
	 * 
	 * @param str
	 *            the String to check
	 * @param prefix
	 *            the prefix to look for
	 * @see java.lang.String#startsWith
	 */
	public static boolean startsWithIgnoreCase(String str, String prefix) {
		if (str == null || prefix == null) {
			return false;
		}
		if (str.startsWith(prefix)) {
			return true;
		}
		if (str.length() < prefix.length()) {
			return false;
		}
		String lcStr = str.substring(0, prefix.length()).toLowerCase();
		String lcPrefix = prefix.toLowerCase();
		return lcStr.equals(lcPrefix);
	}

	/**
	 * 检查 prefix 是否与 str 的最右方匹配，该方法忽略大小写 ignoring upper/lower case.
	 * <p>
	 * 
	 * <pre>
	 * StringUtils.endsWithIgnoreCase(&quot;镇江立 天软件科技 有限公司&quot;,&quot;科技 有限公司&quot;) = true
	 * StringUtils.endsWithIgnoreCase(&quot;镇江立 天软件科技 有限公司&quot;,&quot;科技有限公司&quot;) = false
	 * StringUtils.endsWithIgnoreCase(&quot;abcdefg&quot;,&quot;efg&quot;) = true
	 * StringUtils.endsWithIgnoreCase(&quot;abcdefg&quot;,&quot;Efg&quot;) = true
	 * StringUtils.endsWithIgnoreCase(&quot;abcdefg&quot;,&quot;eFG&quot;) = true
	 * </pre>
	 * 
	 * @param str
	 *            the String to check
	 * @param suffix
	 *            the suffix to look for
	 * @see java.lang.String#endsWith
	 */
	public static boolean endsWithIgnoreCase(String str, String suffix) {
		if (str == null || suffix == null) {
			return false;
		}
		if (str.endsWith(suffix)) {
			return true;
		}
		if (str.length() < suffix.length()) {
			return false;
		}

		String lcStr = str.substring(suffix.length()).toLowerCase();
		String lcSuffix = suffix.toLowerCase();
		return lcStr.equals(lcSuffix);
	}

	/**
	 * 检查字符串sub在str中出现的次数，该方法严格匹配大小写.
	 * 
	 * @param str
	 * @param sub
	 * @return int
	 */
	public static int countOccurrencesOf(String str, String sub) {
		if (str == null || sub == null || str.length() == 0
				|| sub.length() == 0) {
			return 0;
		}
		int count = 0, pos = 0, idx = 0;
		while ((idx = str.indexOf(sub, pos)) != -1) {
			++count;
			pos = idx + sub.length();
		}
		return count;
	}

	/**
	 * J2SE5.0新增特性，可以生成唯一的标识符. 执行一下这段代码,结果有如
	 * 702b0031-d99c-477b-97fa-6cfd158901d6 . 每次执行的结果都不一样.
	 * 
	 * @return String
	 */
	public static String uuid() {
		UUID uuid = UUID.randomUUID();
		return uuid.toString();
	}

	/**
	 * 检查字符串是否有长度
	 * <p>
	 * 
	 * <pre>
	 * StrUtil.hasLength(null) = false
	 * StrUtil.hasLength(&quot;&quot;) = false
	 * StrUtil.hasLength(&quot; &quot;) = true 空格也存在一个长度
	 * StrUtil.hasLength(&quot;Hello&quot;) = true
	 * </pre>
	 * 
	 * @param str
	 *            the String to check, may be <code>null</code>
	 * @return <code>true</code> if the String is not null and has length
	 */
	public static boolean hasLength(String str) {
		return (str != null && str.length() > 0);
	}

	/**
	 * Assert that a string is not empty; that is, it must not be
	 * <code>null</code> and not empty.
	 * 
	 * <pre>
	 * Assert.hasLength(name, &quot;Name must not be empty&quot;);
	 * </pre>
	 * 
	 * @param text
	 *            the string to check
	 * @param message
	 *            the exception message to use if the assertion fails
	 * @see StringUtils#hasLength
	 */
	public static void hasLength(String text, String message) {
		if (!hasLength(text)) {
			throw new IllegalArgumentException(message);
		}
	}

	/**
	 * Assert that an object is not null.
	 * 
	 * <pre>
	 * Assert.notNull(clazz, &quot;The class must not be null&quot;);
	 * </pre>
	 * 
	 * @param object
	 *            the object to check
	 * @param message
	 *            the exception message to use if the assertion fails
	 * @throws IllegalArgumentException
	 *             if the object is <code>null</code>
	 */
	public static void notNull(Object object, String message) {
		if (object == null) {
			throw new IllegalArgumentException(message);
		}
	}

	/**
	 * escape编码
	 * <p>
	 * 
	 * <pre>
	 * StringUtils.escape(&quot;立天软件科技有限公司&quot;)
	 * 返回结果：%u7acb%u5929%u8f6f%u4ef6%u79d1%u6280%u6709%u9650%u516c%u53f8
	 * </pre>
	 * 
	 * @param src
	 *            String
	 * @return String
	 */
	public static String escape(String src) {
		int i;
		char j;
		StringBuffer tmp = new StringBuffer();
		tmp.ensureCapacity(src.length() * 6);
		for (i = 0; i < src.length(); i++) {
			j = src.charAt(i);
			if (Character.isDigit(j) || Character.isLowerCase(j)
					|| Character.isUpperCase(j)) {
				tmp.append(j);
			} else if (j < 256) {
				tmp.append("%");
				if (j < 16) {
					tmp.append("0");
				}
				tmp.append(Integer.toString(j, 16));
			} else {
				tmp.append("%u");
				tmp.append(Integer.toString(j, 16));
			}
		}
		return tmp.toString();
	}

	/**
	 * unescape编码
	 * <p>
	 * 
	 * <pre>
	 * StringUtils.unescape(&quot;%u7acb%u5929%u8f6f%u4ef6%u79d1%u6280%u6709%u9650%u516c%u53f8&quot;)
	 * 返回结果：立天软件科技有限公司
	 * </pre>
	 * 
	 * @param src
	 *            String
	 * @return String
	 */
	public static String unescape(String src) {
		StringBuffer tmp = new StringBuffer();
		tmp.ensureCapacity(src.length());
		int lastPos = 0, pos = 0;
		char ch;
		while (lastPos < src.length()) {
			pos = src.indexOf("%", lastPos);
			if (pos == lastPos) {
				if (src.charAt(pos + 1) == 'u') {
					ch = (char) Integer.parseInt(src
							.substring(pos + 2, pos + 6), 16);
					tmp.append(ch);
					lastPos = pos + 6;
				} else {
					ch = (char) Integer.parseInt(src
							.substring(pos + 1, pos + 3), 16);
					tmp.append(ch);
					lastPos = pos + 3;
				}
			} else {
				if (pos == -1) {
					tmp.append(src.substring(lastPos));
					lastPos = src.length();
				} else {
					tmp.append(src.substring(lastPos, pos));
					lastPos = pos;
				}
			}
		}
		return tmp.toString();
	}

	/**
	 * This method takes a string which may contain HTML tags (ie, &lt;b&gt;,
	 * &lt;table&gt;, etc) and converts the '&lt'' and '&gt;' characters to
	 * their HTML escape sequences 更多的Html转换请使用escapeForSpecial.
	 * 
	 * @param in
	 *            the text to be converted.
	 * @return the input string with the characters '&lt;' and '&gt;' replaced
	 *         with their HTML escape sequences.
	 * @see #escapeForSpecial
	 */
	public static final String escapeHTMLTags(String in) {
		if (in == null) {
			return null;
		}
		char ch;
		int i = 0;
		int last = 0;
		char[] input = in.toCharArray();
		int len = input.length;
		StringBuffer out = new StringBuffer((int) (len * 1.3));
		for (; i < len; i++) {
			ch = input[i];
			if (ch > '>') {
				continue;
			} else if (ch == '<') {
				if (i > last) {
					out.append(input, last, i - last);
				}
				last = i + 1;
				out.append(LT_ENCODE);
			} else if (ch == '>') {
				if (i > last) {
					out.append(input, last, i - last);
				}
				last = i + 1;
				out.append(GT_ENCODE);
			}
		}
		if (last == 0) {
			return in;
		}
		if (i > last) {
			out.append(input, last, i - last);
		}
		return out.toString();
	}

	/**
	 * 
	 * @param string
	 * @return String
	 */
	public static final String escapeForSpecial(String string) {
		if (string == null) {
			return null;
		}
		char ch;
		int i = 0;
		int last = 0;
		char[] input = string.toCharArray();
		int len = input.length;
		StringBuffer out = new StringBuffer((int) (len * 1.3));
		for (; i < len; i++) {
			ch = input[i];
			if (ch > '>') {
				continue;
			} else if (ch == '<') {
				if (i > last) {
					out.append(input, last, i - last);
				}
				last = i + 1;
				out.append(LT_ENCODE);
			} else if (ch == '&') {
				if (i > last) {
					out.append(input, last, i - last);
				}
				last = i + 1;
				out.append(AMP_ENCODE);
			} else if (ch == '"') {
				if (i > last) {
					out.append(input, last, i - last);
				}
				last = i + 1;
				out.append(QUOTE_ENCODE);
			} else if (ch == '>') {
				if (i > last) {
					out.append(input, last, i - last);
				}
				last = i + 1;
				out.append(GT_ENCODE);
			}
		}
		if (last == 0) {
			return string;
		}
		if (i > last) {
			out.append(input, last, i - last);
		}
		return out.toString();
	}

	/**
	 * Escapes all necessary characters in the String so that it can be used in
	 * an XML doc.
	 * 
	 * @param string
	 *            the string to escape.
	 * @return the string with appropriate characters escaped.
	 */
	public static final String escapeForXML(String string) {
		if (string == null) {
			return null;
		}
		char ch;
		int i = 0;
		int last = 0;
		char[] input = string.toCharArray();
		int len = input.length;
		StringBuffer out = new StringBuffer((int) (len * 1.3));
		for (; i < len; i++) {
			ch = input[i];
			if (ch > '>') {
				continue;
			} else if (ch == '<') {
				if (i > last) {
					out.append(input, last, i - last);
				}
				last = i + 1;
				out.append(LT_ENCODE);
			} else if (ch == '&') {
				if (i > last) {
					out.append(input, last, i - last);
				}
				last = i + 1;
				out.append(AMP_ENCODE);
			} else if (ch == '"') {
				if (i > last) {
					out.append(input, last, i - last);
				}
				last = i + 1;
				out.append(QUOTE_ENCODE);
			}
		}
		if (last == 0) {
			return string;
		}
		if (i > last) {
			out.append(input, last, i - last);
		}
		return out.toString();
	}

	/**
	 * 
	 * @param string
	 * @return
	 */
	public static final String unescapeFromXML(String string) {
		string = replace(string, "&lt;", "<");
		string = replace(string, "&gt;", ">");
		string = replace(string, "&quot;", "\"");
		return replace(string, "&amp;", "&");
	}

	/**
	 * 将全角逗号转为半角逗号.
	 * 
	 * @param text
	 * @return
	 */
	public static String replaceComma(String text) {
		if (text != null) {
			text = text.replaceAll("，", ",");
		}
		return text;
	}

	/**
	 * 将"&lt;BR&gt;" 转为"\n"
	 * 
	 * @param text
	 * @return
	 */
	public static String replaceBr(String text) {
		if (text != null) {
			text = text.replaceAll("\n", "<BR>");
		}
		return text;
	}

	/**
	 * 取字符串的前toCount个字符,支持将一个中文字符当作两个长度。
	 * 
	 * @param str
	 * @param toCount
	 * @param suspoint
	 * @return String
	 */
	public static String substring(String str, int toCount, String suspoint) {
		int reInt = 0;
		StringBuffer reStr = new StringBuffer("");
		if (str == null) {
			return "";
		}
		char[] tempChar = str.toCharArray();
		String temp;
		for (int i = 0; (i < tempChar.length && toCount > reInt); i++) {
			temp = String.valueOf(tempChar[i]);
			byte[] b = temp.getBytes();
			reInt += b.length;
			reStr.append(tempChar[i]);
		}
		if (toCount == reInt || (toCount == reInt - 1)) {
			reStr.append(suspoint);
		}
		return reStr.toString();
	}

	/**
	 * 取字符串的前toCount个字符,支持将一个中文字符当作两个长度,以"..."作为省略符。
	 * 
	 * @param str
	 * @param toCount
	 * @return
	 */
	public static String substring(String str, int toCount) {
		return substring(str, toCount, "...");
	}

	/**
	 * 
	 * @param digit
	 * @return
	 */
	public static String getAsciiString(int digit) {
		byte ret[] = new byte[1];
		ret[0] = (byte) digit;
		return new String(ret);
	}

	/**
	 * 
	 * @param s
	 * @return
	 */
	public static int getAsciiNum(String s) {
		if (s.length() < 1) {
			return 0;
		}
		byte b = s.getBytes()[0];
		return b;
	}

	/**
	 * Convenience method to return a Collection as a delimited (e.g. CSV)
	 * String. E.g. useful for toString() implementations.
	 * 
	 * @param coll
	 *            Collection to display
	 * @param delim
	 *            delimiter to use (probably a ",")
	 * @param prefix
	 *            string to start each element with
	 * @param suffix
	 *            string to end each element with
	 */
	public static String collectionToDelimitedString(Collection<String> coll,
			String delim, String prefix, String suffix) {
		if (coll == null) {
			return "";
		}

		StringBuffer sb = new StringBuffer();
		Iterator<String> it = coll.iterator();
		int i = 0;
		while (it.hasNext()) {
			if (i > 0) {
				sb.append(delim);
			}
			sb.append(prefix).append(it.next()).append(suffix);
			i++;
		}
		return sb.toString();
	}

	/**
	 * Convenience method to return a Collection as a delimited (e.g. CSV)
	 * String. E.g. useful for toString() implementations.
	 * 
	 * @param coll
	 *            Collection to display
	 * @param delim
	 *            delimiter to use (probably a ",")
	 */
	public static String collectionToDelimitedString(Collection<String> coll,
			String delim) {
		return collectionToDelimitedString(coll, delim, "", "");
	}

	/**
	 * Take a String which is a delimited list and convert it to a String array.
	 * <p>
	 * A single delimiter can consists of more than one character: It will still
	 * be considered as single delimiter string, rather than as bunch of
	 * potential delimiter characters - in contrast to
	 * <code>tokenizeToStringArray</code>.
	 * 
	 * @param str
	 *            the input String
	 * @param delimiter
	 *            the delimiter between elements (this is a single delimiter,
	 *            rather than a bunch individual delimiter characters)
	 * @return an array of the tokens in the list
	 */
	public static String[] delimitedListToStringArray(String str,
			String delimiter) {
		if (str == null) {
			return new String[0];
		}
		if (delimiter == null) {
			return new String[] { str };
		}

		List<String> result = new ArrayList<String>();
		int pos = 0;
		int delPos = 0;
		while ((delPos = str.indexOf(delimiter, pos)) != -1) {
			result.add(str.substring(pos, delPos));
			pos = delPos + delimiter.length();
		}
		if (str.length() > 0 && pos <= str.length()) {
			// Add rest of String, but not in case of empty input.
			result.add(str.substring(pos));
		}

		return (String[]) result.toArray(new String[result.size()]);
	}

	/**
	 * Normalize the path by suppressing sequences like "path/.." and inner
	 * simple dots.
	 * <p>
	 * The result is convenient for path comparison. For other uses, notice that
	 * Windows separators ("\") are replaced by simple dashes.
	 * 
	 * @param path
	 *            the original path
	 * @return the normalized path
	 */
	public static String cleanPath(String path) {
		String pathToUse = replace(path, WINDOWS_FOLDER_SEPARATOR,
				FOLDER_SEPARATOR);

		// Strip prefix from path to analyze, to not treat it as part of the
		// first path element. This is necessary to correctly parse paths like
		// "file:core/../core/io/Resource.class", where the ".." should just
		// strip the first "core" directory while keeping the "file:" prefix.
		int prefixIndex = pathToUse.indexOf(":");
		String prefix = "";
		if (prefixIndex != -1) {
			prefix = pathToUse.substring(0, prefixIndex + 1);
			pathToUse = pathToUse.substring(prefixIndex + 1);
		}

		String[] pathArray = delimitedListToStringArray(pathToUse,
				FOLDER_SEPARATOR);
		List<String> pathElements = new LinkedList<String>();
		int tops = 0;
		for (int i = pathArray.length - 1; i >= 0; i--) {
			if (CURRENT_PATH.equals(pathArray[i])) {
				// do nothing
			} else if (TOP_PATH.equals(pathArray[i])) {
				tops++;
			} else {
				if (tops > 0) {
					tops--;
				} else {
					pathElements.add(0, pathArray[i]);
				}
			}
		}

		return prefix
				+ collectionToDelimitedString(pathElements, FOLDER_SEPARATOR);
	}

	/**
	 * Compare two paths after normalization of them.
	 * 
	 * @param path1
	 *            First path for comparizon
	 * @param path2
	 *            Second path for comparizon
	 * @return True if the two paths are equivalent after normalization
	 */
	public static boolean pathEquals(String path1, String path2) {
		return cleanPath(path1).equals(cleanPath(path2));
	}

	public static String formatPrice(double value, String format) {
		if ("".equals(getNullString(format))) {
			format = "#.00";
		}
		DecimalFormat df = new DecimalFormat(format);
		return df.format(value);
	}

	public static String formatPrice(double value) {
		return formatPrice(value,"#.00");
	}

	public static String formatPrice(String value) {
		String subval = "0.00";
		if (getNullString(value).equals("")) {
			return subval;
		}
		return formatPrice(Double.parseDouble(value));
	}

	public static double formatPrice(double v, int scale) {
		if (scale < 0) {
			throw new IllegalArgumentException(
					"The scale must be a positive integer or zero");
		}
		BigDecimal b = new BigDecimal(Double.toString(v));
		BigDecimal one = new BigDecimal("1");
		return b.divide(one, scale, BigDecimal.ROUND_HALF_UP).doubleValue();
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// 测试deleteAll
		// String temp = "什么WEeB重构技术";
		// String del = "E技";
		// System.out.println(delete(temp, del));
		// 测试countOccurrencesOf
		// String temp = "什么WEeB重构重技术";
		// String sub = "重";
		// System.out.println(countOccurrencesOf(temp, sub));
		// System.out.println(escapeForSpecial("abcdfg\"sfdfaeeed<a
		// href=>ghjfghj"));
		// System.out.println(substring("什么WEeB重构重技术", 10, ".."));
		// System.out.println(getAsciiString(113));
		// System.out.println(getAsciiNum("q"));
		// double d = 78.53;
		// double d3 = 56.857653;
		// System.out.println(d/d3);
		// System.out.println(formatPrice(d3,4));

		// int a = 50;
		// int b = 163;
		// System.out.println("(b%a)==0 : " + ((b%a)==0));
		// System.out.println("(b/a) : " + (b/a));
		// System.out.println("(b%a) : " + (b%a));

		double d1 = 234.3455343;
		DecimalFormat df = new DecimalFormat("#.000");
		System.out.println(df.format(d1));
		System.out.println(formatPrice("6.3"));

	}

}
